package com.miku.sys.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.miku.common.BaseServiceImpl;
import com.miku.common.PageControl;
import com.miku.db.sys.entity.SysUser;
import com.miku.sys.config.Global;
import com.miku.sys.dao.SysUserDaoI;
import com.miku.sys.security.utils.SessionDAO;
import com.miku.sys.utils.UserUtils;
import com.miku.utils.DateUtils;
import com.miku.utils.PairResult;

/**
 * 系统管理，安全相关实体的管理类,包括用户、角色、菜单.
 */
@Service
@Transactional
public class SystemServiceImpl extends BaseServiceImpl implements SystemServiceI {
	@Autowired private SessionDAO sessionDao;
	@Autowired SysUserDaoI userDao;
	
	@Override
	public SessionDAO getSessionDao() {
		return sessionDao;
	}

	/**
	 * 获取Key加载信息
	 */
	public static void printKeyLoadMessage(){
		StringBuilder sb = new StringBuilder();
		sb.append("\r\n======================================================================\r\n");
		sb.append("\r\n    欢迎进入 "+ Global.getConfig("productName") + "  - Powered By " + Global.getConfig("poweredBy") + "\r\n");
		sb.append("\r\n======================================================================\r\n");
		System.out.println(sb.toString());
	}
	
	public static void printLoadBeginMessage(){
		System.out.println(">>>>>>[" + DateUtils.getDateTime() + "] 系统正在启动，请稍后.....");
	}

	@Override
	public SysUser findByLoginName(String name) {
		System.out.println("查找用户。。。findByLoginName begin..." + DateUtils.getDateTime());
		SysUser u = userDao.findByLoginName(name);
		System.out.println("查找用户。。。findByLoginName end..." + DateUtils.getDateTime());
		return u; 
	}

	@Override
	public void updateUserLoginInfo(SysUser user) {
		UserUtils.updateUserLoginInfo(user);
		userDao.update(user);
	}

	@Override
	public void updatePassword(SysUser user, String newPassword) {
		UserUtils.updatePassword(user, newPassword);
	}
	
	@Override
	public SysUser findByLoginId(int userID) {
		return userDao.findByID(userID);
	}

	@Override
	public List<SysUser> listAllUser() {
		return userDao.list();
	}

	/**
	 * 所有用户的分页
	 */
	@Override
	public PairResult<List<SysUser>> getListUser(int pageShowRows ,int pageIndex, int showPages) {
		PairResult<List<SysUser>> result = new PairResult<List<SysUser>>();
		int countRows = userDao.countUser();
		PageControl page = new PageControl(pageShowRows, null, countRows,showPages);
		page.to(pageIndex);
		result.setPage(page);
		result.setValue(userDao.getListUser(page.getFirstResults(), page.getMaxResults()));
		
		return result;
	}
	
	/**
	 * 禁止用户的分页
	 */
	@Override
	public PairResult<List<SysUser>> listNoLoginUser(int pageShowRows ,int pageIndex, int showPages, String allowLogin) {
		PairResult<List<SysUser>> result = new PairResult<List<SysUser>>();
		int countRows = userDao.countNoLoginUser(allowLogin);
		PageControl page = new PageControl(pageShowRows, null, countRows,showPages);
		page.to(pageIndex);
		result.setPage(page);
		result.setValue(userDao.listNoLoginUser(page.getFirstResults(), page.getMaxResults(), allowLogin));
		
		return result;
	}
	
	
	 /**
	  * 在线用户的分页
	  * @param pageShowRows
	  * @param pageIndex
	  * @param showPages
	  * @param ids
	  * @return
	  */
	@Override
	public PairResult<List<SysUser>> listOnlineUser(int pageShowRows ,int pageIndex, int showPages, List<Integer> ids) {
		PairResult<List<SysUser>> result = new PairResult<List<SysUser>>();
		int countRows = ids.size();
		PageControl page = new PageControl(pageShowRows, null, countRows,showPages);
		page.to(pageIndex);
		result.setPage(page);
		result.setValue(userDao.listOnlineUser(page.getFirstResults(), page.getMaxResults(), ids));
		
		return result;
	}
	
	/**
	 * 修改用户登入权限
	 */
	@Override
	public void modifyAllowLogin(String state ,int userID){
		userDao.modifyAllowLogin(state, userID);
	}

	@Override
	public void updateNickname(String nickname,int id) {
		userDao.mergeNickName(nickname, id);
	}
}
